﻿using MaterialTypeRecognition.Shell.Uers;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace MaterialTypeRecognition.Shell.EntityFrameworkCore
{
    public class MyAppDbContext : ApplicationDbContext<MyAppDbContext>
    {
#pragma warning disable CS8618 // 在退出构造函数时，不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
        public MyAppDbContext(DbContextOptions<MyAppDbContext> options, IConfiguration configuration) : base(options)
#pragma warning restore CS8618 // 在退出构造函数时，不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
        {
            _configuration = configuration;
        }

        private readonly IConfiguration _configuration;

        public DbSet<User> Users { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<User>(typeBuilder =>
            {
                typeBuilder.ToTable("Users", t=>t.HasComment("用户表"));
                typeBuilder.HasKey(x => x.Id);
                typeBuilder.HasIndex(x => x.Id);
                typeBuilder.Property(x => x.UserName).HasComment("用户名");
                typeBuilder.HasIndex(x => x.Key).IsUnique();
            });

            // 添加数据种子。
            builder.ApplyConfiguration(new MyEntityConfiguration());
            base.OnModelCreating(builder);
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite(_configuration.GetConnectionString("DefaultConnection"));
            base.OnConfiguring(optionsBuilder);
        }
    }
}
